package com.fulu.game.bi.service.impl.room;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.fulu.game.bi.common.util.csv.CsvExportUtil;
import com.fulu.game.bi.common.util.csv.Mapper;
import com.fulu.game.bi.common.util.csv.Querier;
import com.fulu.game.bi.entity.bo.channel.VoiceGuildRoomListBO;
import com.fulu.game.bi.entity.dto.VoiceGuildRoomListDTO;
import com.fulu.game.bi.entity.enums.QueryTypeStatusEnum;
import com.fulu.game.bi.entity.vo.room.VoiceGuildRoomLineVO;
import com.fulu.game.bi.entity.vo.room.VoiceGuildRoomVO;
import com.fulu.game.bi.hbase.HBaseRoomStatAllUserDAO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class VoiceGuildRoomService extends BaseVoiceService {

    @Autowired
    private HBaseRoomStatAllUserDAO hBaseRoomStatAllUserDAO;

    public PageInfo<VoiceGuildRoomVO> list(VoiceGuildRoomListDTO voiceGuildRoomListDTO) {
        VoiceGuildRoomListBO voiceGuildRoomListBO = new VoiceGuildRoomListBO();
        BeanUtil.copyProperties(voiceGuildRoomListDTO, voiceGuildRoomListBO);
        voiceGuildRoomListBO.setTableName(tableNameMap.get(voiceGuildRoomListDTO.getQueryType()));
        voiceGuildRoomListBO.setDataFromStr(voiceGuildRoomListDTO.getDataFrom() == null ? null : voiceGuildRoomListDTO.getDataFrom() + "");
        voiceGuildRoomListBO.setRoomCategoryIdStr(voiceGuildRoomListDTO.getRoomCategoryId() == null ? null : voiceGuildRoomListDTO.getRoomCategoryId() + "");
        voiceGuildRoomListBO.setPageNo((voiceGuildRoomListDTO.getPageNum() - 1) * voiceGuildRoomListDTO.getPageSize());
        List<VoiceGuildRoomVO> list = hBaseRoomStatAllUserDAO.guildRoomList(voiceGuildRoomListBO);
        Integer count = hBaseRoomStatAllUserDAO.countForGuildRoomList(voiceGuildRoomListBO);
        PageInfo<VoiceGuildRoomVO> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(count == null ? 0 : count);
        List<VoiceGuildRoomVO> voiceGuildRoomVOS = list.stream().map(map -> {
            QueryTypeStatusEnum anEnum = QueryTypeStatusEnum.getEnum(voiceGuildRoomListDTO.getQueryType());
            switch (anEnum) {
                case DAY:
                    map.setStartDate(map.getStatDate());
                    map.setEndDate(map.getStatDate());
                    break;
                case WEEK:
                    map.setStartDate(map.getStatDate());
                    map.setEndDate(DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parse(map.getStatDate()), 6)));
                    break;
                case MONTH:
                    map.setStartDate(map.getStatDate() + "-01");
                    map.setEndDate(DateUtil.formatDate(DateUtil.offsetDay(DateUtil.offsetMonth(DateUtil.parse(map.getStatDate() + "-01"), 1), -1)));
                    break;
                default:
                    break;
            }
            map.setGuildCode(Objects.equals(map.getGuildCode(), "0") ? null : map.getGuildCode());
            return map;
        }).collect(Collectors.toList());
        PageInfo<VoiceGuildRoomVO> voPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo, voPageInfo);
        voPageInfo.setList(voiceGuildRoomVOS);
        return voPageInfo;
    }

    public void export(VoiceGuildRoomListDTO dto) {
        String fileName = "语言互动-公会房间";
        LinkedHashMap<String, Mapper<VoiceGuildRoomVO>> map = new LinkedHashMap<>();
        QueryTypeStatusEnum anEnum = QueryTypeStatusEnum.getEnum(dto.getQueryType());
        switch (anEnum) {
            case DAY:
                map.put("日期", v -> v.getStartDate());
                break;
            default:
                map.put("日期", v -> v.getStartDate() + "-" + v.getEndDate());
                break;
        }
        map.put("房间ID", v -> v.getRoomNo());
        map.put("房间名称", v -> v.getRoomName());
        map.put("房间类型", v -> v.getRoomCategory());
        map.put("公会ID", v -> v.getGuildCode());
        map.put("公会名称", v -> v.getGuildName());
        map.put("曝光人数", v -> v.getExposureUserNum() + "");
        map.put("曝光次数", v -> v.getExposureNum() + "");
        map.put("钻石消费金额", v -> v.getVirtualMoneyAll().toString());
        map.put("消费人数", v -> v.getConsumeUserNum() + "");
        map.put("消费次数", v -> v.getConsumeNum() + "");
        map.put("魅力值流水", v -> v.getCharmMoney().toString());
        map.put("CVR", v -> v.getCvr().toString());
        map.put("进厅人数", v -> v.getEnterRoomUserNum() + "");
        map.put("进厅次数", v -> v.getEnterRoomNum() + "");
        map.put("CTR", v -> v.getCtr().toString());
        map.put("新增收藏", v -> v.getAddCollectNum() + "");
        map.put("新增关注", v -> v.getAddFollowNum() + "");
        map.put("用户平均停留时长", v -> v.getUserStayTimeAvg() + "");
        map.put("停留>5分钟用户数", v -> v.getStayFiveMinUserNum() + "");
        map.put("互动用户数", v -> v.getInteractUserNum() + "");
        map.put("开厅时长", v -> v.getOpenTimeDuration().toString());
        Querier querier = (num) -> list(dto);
        CsvExportUtil.export(100, fileName, map, querier);
    }

    public List<VoiceGuildRoomLineVO> listLine(VoiceGuildRoomListDTO voiceGuildRoomListDTO) {
        VoiceGuildRoomListBO voiceGuildRoomListBO = new VoiceGuildRoomListBO();
        BeanUtil.copyProperties(voiceGuildRoomListDTO, voiceGuildRoomListBO);
        voiceGuildRoomListBO.setTableName(tableNameMap.get(voiceGuildRoomListDTO.getQueryType()));
        voiceGuildRoomListBO.setDataFromStr(voiceGuildRoomListDTO.getDataFrom() == null ? null : voiceGuildRoomListDTO.getDataFrom() + "");
        voiceGuildRoomListBO.setRoomCategoryIdStr(voiceGuildRoomListDTO.getRoomCategoryId() == null ? null : voiceGuildRoomListDTO.getRoomCategoryId() + "");
        List<VoiceGuildRoomLineVO> list = hBaseRoomStatAllUserDAO.guildRoomListLine(voiceGuildRoomListBO);
        return list.stream().map(map -> {
            map.setQueryType(voiceGuildRoomListDTO.getQueryType());
            return map;
        }).collect(Collectors.toList());
    }
}
